package com.eazyimage.lab.service;


import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.List;
import java.util.zip.ZipOutputStream;
import java.util.zip.ZipEntry;

public class FileUtils {
    public static void zipFile(File inFile, ZipOutputStream zos, String dir) throws IOException {
        if (inFile.isDirectory()) {
            File[] files = inFile.listFiles();
            for (File file : files)
                zipFile(file, zos, dir + "\\" + inFile.getName());
        } else {
            String entryName = null;
            if (!"".equals(dir))
                entryName = dir + "\\" + inFile.getName();
            else
                entryName = inFile.getName();
            ZipEntry entry = new ZipEntry(entryName);
            zos.putNextEntry(entry);
            InputStream is = new FileInputStream(inFile);
            int len = 0;
            while ((len = is.read()) != -1)
                zos.write(len);
            is.close();
        }
    }

    public static void deleteFile(File file){
        if(file.isDirectory()){
            File[] subFiles = file.listFiles();
            if(subFiles != null){
                for(File subFile:subFiles){
                    deleteFile(subFile);
                }
            }
            if (file.exists())
                file.delete();
        }else{
            if(file.exists())
                file.delete();
        }
    }

    public static class RHDel implements Runnable {
        private String filename;

        public RHDel(String filename) {
            this.filename = filename;
        }

        @Override
        public void run() {
            try {
                System.out.println(filename+" 将在 1 小时后删除");
                Thread.sleep(1000*60*60);
                FileUtils.deleteFile(new File(this.filename));
                System.out.println(filename+" 已经删除");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}






















